<template>
  <div class="cate">
    <div class="title">
      <slot name="title"></slot>
    </div>
    <div style="padding: 10px">
      <slot name="input"></slot>
      <slot name="button"></slot>
    </div>
    <el-table class="cateTable" ref="singleTableRef" v-loading="loading" element-loading-text="加载中..." :data="cateList"
      height="100%" @row-click="rowClick" :highlight-current-row="true">
      <slot name="table"></slot>
      <el-table-column prop="statusName" label="全部分类" width="85" align="center">
        <template #header>
          <el-link v-if="showall" :underline="false" type="primary" @click="setCurrent()">{{
            '全部' + name
          }}</el-link>
        </template>
        <template #default="scope">
          <el-dropdown>
            <el-icon style="cursor: pointer" :size="20">
              <Tools />
            </el-icon>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleEdit(scope.row)">{{
                  '编辑' + name
                }}</el-dropdown-item>
                <el-dropdown-item @click="handleDel(scope.row)">{{
                  '删除' + name
                }}</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </el-table-column>
      <slot></slot>
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { ElTable } from 'element-plus'
import openMsgBox from '@/hooks/messageBox.js'
import store from '@/store'
import { commonService } from '@/api/common'
const props = defineProps({
  handleEdit: {
    type: Function,
  },
  selectItem: {
    type: Object,
  },
  rowClick: {
    type: Function,
  },
  getListFn: {
    type: Function,
  },
  type: {
    type: String,
  },
  dictionaryKey: {
    type: String,
  },
  filterText: {
    type: String,
  },
  searchParams: {
    type: Object,
  },
  showall: {
    type: Boolean,
    default: true,
  },

})
const name = props.type === 'cate' ? '分类' : '类型'
const loading = ref(false)
const cateList = ref([])
const currtRow = ref(null)
const cateFilterList = ref([])
const filterNode = (val) => {
  cateList.value = cateFilterList.value.filter((e) => {
    return e.dictionaryName.includes(val)
  })
}
watch(
  () => props.filterText,
  (val) => {
    if (val) {
      filterNode(val)
    } else {
      cateList.value = cateFilterList.value
    }
  }
)

const handleEdit = (row) => {
  
  props.handleEdit(row)
}
const handleDel = (row) => {

  openMsgBox(
    { tips: '删除分类后，分类下的内容将一起删除。确定要删除吗' },
    () => {
      return commonService.delDictionary({
        data: row.dictionaryId,
      })
    },
    () => {

      getCateList()
    }
  )
}
const rowClick = (e) => {
  currtRow.value = e;
 
  props.rowClick(e)
}

const getCateList = async () => {
  loading.value = true
  try {

    if (props.type == 'cate') {
      const { data } = await props.getListFn({
        data: props.dictionaryKey,
      })
      cateList.value = data
      cateFilterList.value = data
      store.commit(
        'cates/SET_CATELIST',
        data.map((e) => {
          return {
            label: e.dictionaryName,
            value: e.dictionaryValue,
          }
        })
      )
      emit('update:filterText', '')
      if (currtRow.value) {
        const currentData = cateFilterList.value.find((n) => n.dictionaryId == currtRow.value.dictionaryId)

        if (currentData) {
          nextTick(() => {
            singleTableRef.value.setCurrentRow(currentData)
            //props.rowClick(currentData)
          })

        } else {
          props.rowClick(null)
        }
      }


    } else {
      const { data } = await props.getListFn({
        data: { ...props.searchParams },
      })
      cateList.value = data
      nextTick(() => {
        singleTableRef.value.setCurrentRow(cateList.value[0] || '')
        //props.rowClick(cateList.value[0])
      })
    }
  } catch (error) {
    cateList.value = []
  }
  loading.value = false
}

const emit = defineEmits(['update:searchParams', 'update:filterText'])
const singleTableRef = ref<InstanceType<typeof ElTable>>()
const setCurrent = (row?) => {
  singleTableRef.value!.setCurrentRow(row)
  if (props.type == 'cate') {
    emit('update:filterText', '')
  } else {
    emit('update:searchParams', {})
  }
  props.rowClick(null)
}
onMounted(() => {
  getCateList()
  
})
defineExpose({ getCateList, cateList })
</script>

<style lang="scss" scoped>
.cate {
  display: flex;
  flex-direction: column;
  background-color: #ffff;
  width: 270px;
  height: 100%;
  margin-right: 10px;
  border-right: 1px solid #e9e9e9;
  box-sizing: border-box;

  .title {
    padding: 8px 0;
    border-bottom: 1px solid #e9e9e9;
    text-align: center;
  }
}

:deep(.cateTable) {
  flex: 1;

  .el-table__row {
    cursor: pointer;
  }

  thead {
    color: #aaa !important;

    tr>.el-table__cell {
      background-color: #fff !important;
    }
  }
}
</style>
